package common 
{
	import config.FILE_URL;
	import nsdecl.ns_control_model;
	import nsdecl.ns_fileurl_const;
	
	/**
	 * ...
	 * @author anod
	 */
	public class Struct implements IData 
	{
		use namespace ns_control_model;
		use namespace ns_fileurl_const;
		
		ns_control_model var mStaffVector:Vector.<StructStaff>;
		ns_control_model var mGroupVector:Vector.<StructGroup>;
		ns_control_model var mTimesegVector:Vector.<StructTime>;
		
		public function Struct() 
		{
			mStaffVector   = new Vector.<StructStaff>();
			mGroupVector   = new Vector.<StructGroup>();
			mTimesegVector = new Vector.<StructTime>();
		}
		
		/* INTERFACE common.IData */
		
		public function loadFromXML(xml:XML):void 
		{
			loadStaff( xml[FILE_URL.STAFF_NODE][0] );
			loadGroup( xml[FILE_URL.GROUP_NODE][0] );
			loadTimeseg( xml[FILE_URL.TIME_NODE][0] );
		}
		
		private function loadStaff( xml:XML ):void
		{
			for each( var staff:XML in xml.item )
			{
				var entity:StructStaff = new StructStaff( staff.childIndex() );
				entity.loadFromXML( staff );
				mStaffVector.push( entity );
			}
		}
		
		private function loadGroup( xml:XML ):void
		{
			for each( var group:XML in xml.item )
			{
				var entity:StructGroup = new StructGroup( group.childIndex() );
				entity.loadFromXML( group );
				mGroupVector.push( entity );
			}
		}
		
		private function loadTimeseg( xml:XML ):void
		{
			for each( var timeseg:XML in xml.item )
			{
				var entity:StructTime = new StructTime( timeseg.childIndex() );
				entity.loadFromXML( timeseg );
				mTimesegVector.push( entity );
			}
		}
		
		public function toXML():String 
		{
			return "<Database>\n" + getStaffAsXML() + "\n" + getGroupAsXML() + "\n" + getTimesegAsXML() + "\n</Database>";
		}
		
		public function getStaffAsXML():String
		{
			var xml_str:String = "<" + FILE_URL.STAFF_NODE + ">\n";
			for each( var staff:StructStaff in mStaffVector )
				xml_str += "\t"+staff.toXML() + "\n";
			xml_str += "</" + FILE_URL.STAFF_NODE + ">";
			return xml_str;
		}
		public function getGroupAsXML():String
		{
			var xml_str:String = "<" + FILE_URL.GROUP_NODE + ">\n";
			for each( var group:StructGroup in mGroupVector )
			{
				var members:Array = [];
				for each( var id:int in group.members )
				{
					var pos:int = search( mStaffVector, id );
					if ( pos >= 0 ) members.push( pos );
				}
				xml_str += '\t<item name="' + group.name + '">' + members.join(",") + '</item>\n';
			}
			xml_str += "</" + FILE_URL.GROUP_NODE + ">";
			return xml_str;
			
			function search( v:Vector.<StructStaff>, id:int ):int
			{
				var idx:int = -1, len:int = v.length;
				for ( var i:int = 0; i < len; ++i )
				{
					if ( id == v[i].ID ) idx = i;
				}
				return idx;
			}
		}
		public function getTimesegAsXML():String
		{
			var xml_str:String = "<" + FILE_URL.TIME_NODE + ">\n";
			for each( var timeseg:StructTime in mTimesegVector )
				xml_str += "\t"+timeseg.toXML() + "\n";
			xml_str += "</" + FILE_URL.TIME_NODE + ">";
			return xml_str;
		}
		
	}

}